package task

import (
	"time"
)

// TaskStatus 任务状态枚举
type TaskStatus string

const (
	TaskStatusPending    TaskStatus = "pending"
	TaskStatusProcessing TaskStatus = "processing"
	TaskStatusCompleted  TaskStatus = "completed"
	TaskStatusFailed     TaskStatus = "failed"
	TaskStatusCancelled  TaskStatus = "cancelled"
	TaskStatusRetry      TaskStatus = "retry"
)

// Task 上传任务模型
type Task struct {
	TaskID       string     `json:"task_id" gorm:"column:task_id;primaryKey"`
	BatchID      string     `json:"batch_id" gorm:"column:batch_id"`
	SourceType   string     `json:"source_type" gorm:"column:source_type;not null"`
	SourceName   string     `json:"source_name" gorm:"column:source_name;not null"`
	DataCount    int        `json:"data_count" gorm:"column:data_count;not null;default:0"`
	FileCount    int        `json:"file_count" gorm:"column:file_count;not null;default:0"`
	TotalSize    int64      `json:"total_size" gorm:"column:total_size;not null;default:0"`
	Status       TaskStatus `json:"status" gorm:"column:status;not null;default:'pending'"`
	Priority     int8       `json:"priority" gorm:"column:priority;not null;default:5"`
	RetryCount   int8       `json:"retry_count" gorm:"column:retry_count;not null;default:0"`
	ErrorMessage string     `json:"error_message" gorm:"column:error_message"`
	StartedAt    *time.Time `json:"started_at" gorm:"column:started_at"`
	CompletedAt  *time.Time `json:"completed_at" gorm:"column:completed_at"`
	CreatedAt    time.Time  `json:"created_at" gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
	UpdatedAt    time.Time  `json:"updated_at" gorm:"column:updated_at;default:CURRENT_TIMESTAMP"`
}

// TableName 指定表名
func (Task) TableName() string {
	return "upload_tasks"
}

// TaskDetail 任务详情模型
type TaskDetail struct {
	ID                int64      `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
	TaskID            string     `json:"task_id" gorm:"column:task_id;not null"`
	DataID            string     `json:"data_id" gorm:"column:data_id;not null"`
	DataType          string     `json:"data_type" gorm:"column:data_type;not null"`
	MongoDBDatabase   string     `json:"mongodb_database" gorm:"column:mongodb_database;not null"`
	MongoDBCollection string     `json:"mongodb_collection" gorm:"column:mongodb_collection;not null"`
	ESIndex           string     `json:"es_index" gorm:"column:es_index;not null"`
	FileRefs          string     `json:"file_refs" gorm:"column:file_refs;type:json"`
	Status            string     `json:"status" gorm:"column:status;not null;default:'pending'"`
	ErrorMessage      string     `json:"error_message" gorm:"column:error_message"`
	ProcessedAt       *time.Time `json:"processed_at" gorm:"column:processed_at"`
	CreatedAt         time.Time  `json:"created_at" gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
}

// TableName 指定表名
func (TaskDetail) TableName() string {
	return "task_details"
}
